package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println("时间包")
	now := time.Now()
	fmt.Println(now.Year())
	fmt.Println(now.Month())
	fmt.Println(now.Day())
	fmt.Println(now.Hour())
	fmt.Println(now.Minute())
	fmt.Println(now.Second())
	//时间戳
	fmt.Println(now.Unix())
	fmt.Println(now.UnixNano())
	//time.Unix
	ret := time.Unix(1586567939, 0)
	fmt.Println(ret)
	//时间间隔
	fmt.Println(time.Second)
	fmt.Println(now.Add(24 * time.Hour))
	//计算时间间隔
	zuotian, err := time.Parse("2006-01-02", "2020-04-10")
	if err != nil {
		fmt.Printf("err :%v\n", err)
	}
	d := now.Sub(zuotian)
	fmt.Println(d)
	fmt.Println("-----------")
	//定时器
	// time1 := time.Tick(time.Second)
	// for t := range time1 {
	// 	fmt.Println(t) //1秒执行一次
	// }
	//时间格式化
	fmt.Println(now.Format("2006-01-02 15:04:05.000 Mon Jan"))
	// 12小时制
	fmt.Println(now.Format("2006-01-02 03:04:05.000 PM Mon Jan"))
	fmt.Println(now.Format("2006/01/02 15:04"))
	fmt.Println(now.Format("15:04 2006/01/02"))
	fmt.Println(now.Format("2006/01/02"))
	//按照对应的格式字符串类型的时间
	timeobj, err := time.Parse("2006-01-02", "2020-04-10")
	if err != nil {
		fmt.Printf("err :%v\n", err)
	}
	fmt.Println(timeobj)
	fmt.Println(timeobj.Unix())
	//sleep
	// n := 5 //int
	// fmt.Println("开始睡觉")
	// time.Sleep(time.Duration(n) * time.Second)
	// time.Sleep(5 * time.Second)
	// fmt.Println("5秒过去了")

	//按照东八区时间格式计算字符串格式
	loc, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		fmt.Printf("err :%v\n", err)
		return
	}
	timeobj1, err := time.ParseInLocation("2006-01-02 15:04:05", "2020-04-12 15:04:05", loc)
	if err != nil {
		fmt.Printf("err : %v\n", err)
		return
	}
	fmt.Println(timeobj1)

	td := timeobj1.Sub(now)
	fmt.Println(td)
}
